﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Task_Network_Center.General;


namespace GCGMain
{
    /// <summary>
    /// Logique d'interaction pour FrmConfigGeneral.xaml
    /// </summary>
    public partial class FrmConfigGeneral : Window
    {

        RegistryKey Key = Registry.CurrentUser.OpenSubKey
        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        public object[,] TempEmployeArray;


        public FrmConfigGeneral()
        {
            InitializeComponent();
        }

        private void loadingConfig()
        {
            int serveurStartIndex = 0;
            int userStartIndex = 0;
            int passStartIndex = 0;
            int dataStartIndex = 0;

            string search = "";
            string loadedServeur = "";
            string loadedUser = "";
            string loadedPass = "";
            string loadedData = "";


            serveurStartIndex = Properties.Settings.Default.connectionString.LastIndexOf("server=") + 6;
            while (search != ";")
            {
                serveurStartIndex++;
                loadedServeur = loadedServeur + Properties.Settings.Default.connectionString.Substring(serveurStartIndex, 1);
                search = Properties.Settings.Default.connectionString.Substring(serveurStartIndex + 1, 1);
            }

            GCGGeneralLib.GeneralVar.Server = loadedServeur;
            search = "";

            userStartIndex = Properties.Settings.Default.connectionString.LastIndexOf("User Id=") + 7;
            while (search != ";")
            {
                userStartIndex++;
                loadedUser = loadedUser + Properties.Settings.Default.connectionString.Substring(userStartIndex, 1);
                search = Properties.Settings.Default.connectionString.Substring(userStartIndex + 1, 1);
            }

            GCGGeneralLib.GeneralVar.DbUser = loadedUser;
            search = "";

            passStartIndex = Properties.Settings.Default.connectionString.LastIndexOf("password=") + 8;
            while (search != ";")
            {
                passStartIndex++;

                loadedPass = loadedPass + Properties.Settings.Default.connectionString.Substring(passStartIndex, 1);
                search = Properties.Settings.Default.connectionString.Substring(passStartIndex + 1, 1);
            }
            GCGGeneralLib.GeneralVar.DbPassword = loadedPass;

            dataStartIndex = Properties.Settings.Default.connectionString.LastIndexOf("database=") + 8;


            dataStartIndex++;
            loadedData = loadedData + Properties.Settings.Default.connectionString.Substring(dataStartIndex, Properties.Settings.Default.connectionString.Length - dataStartIndex);
            GCGGeneralLib.GeneralVar.Database = loadedData;
            search = "";
        }

        private void chkInfo_Checked(object sender, RoutedEventArgs e)
        {
            txtDelais.IsEnabled = true;

        }

        private void chkInfo_Unchecked(object sender, RoutedEventArgs e)
        {
            txtDelais.IsEnabled = false;
        }

        private void ConfigGeneral_Loaded(object sender, RoutedEventArgs e)
        {
            chkInfo.IsChecked = Properties.Settings.Default.notify;
            chkStartUp.IsChecked = Properties.Settings.Default.isStartUp;
            txtDelais.Text = Properties.Settings.Default.notifyDelay.ToString();
            txtServer.Text = GCGGeneralLib.GeneralVar.Server;
            txtUser.Text = GCGGeneralLib.GeneralVar.DbUser;
            txtPass.Password = GCGGeneralLib.GeneralVar.DbPassword;
            txtTable.Text = GCGGeneralLib.GeneralVar.Database;
        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                if (Convert.ToInt32(txtDelais.Text) > 1)
                {
                    Properties.Settings.Default.notify = chkInfo.IsChecked.Value;
                    Properties.Settings.Default.isStartUp = chkStartUp.IsChecked.Value;
                    Properties.Settings.Default.notifyDelay = Convert.ToInt32(txtDelais.Text);
                    Properties.Settings.Default.Save();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Le délai de notification doit être au minimum 1 minute.");
                    txtDelais.Text = "1";
                }
               
            }
            catch (Exception x)
            {
                MessageBox.Show("Le délai de notification doit être un nombre entier." + x);
                txtDelais.Text = "5";
            }
            

        }

        private void cmdQuit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void chkStartUp_Checked(object sender, RoutedEventArgs e)
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            path = path.Substring(6, path.Length - 6); // enlever file://
            Key.SetValue("GCGSuite", "\"" + path + "\\GCG-Suite.exe\"");
        }

        private void chkStartUp_Unchecked(object sender, RoutedEventArgs e)
        {
            Key.DeleteValue("GCGSuite"); 
        }

        private void cmdConnectionTest_Click(object sender, RoutedEventArgs e)
        {
            if (testLogIn())
            {

                string newConnection = Properties.Settings.Default.connectionString;
                newConnection = newConnection.Replace("server=" + GCGGeneralLib.GeneralVar.Server, "server=" + txtServer.Text);
                newConnection = newConnection.Replace("User Id=" + GCGGeneralLib.GeneralVar.DbUser, "User Id=" + txtUser.Text);
                newConnection = newConnection.Replace("password=" + GCGGeneralLib.GeneralVar.DbPassword, "password=" + txtPass.Password);
                newConnection = newConnection.Replace("database=" + GCGGeneralLib.GeneralVar.Database, "database=" + txtTable.Text);
                Properties.Settings.Default.connectionString = newConnection;

                // ID serre a rien a ce que je sache. Utiliser Initial (String) a place pcq si il change de user icic ont le c pas jusquau prochain demarrage
                for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                {
                    if (TempEmployeArray[1, i].ToString() == txtUser.Text.Substring(9, 3))
                    {
                        Properties.Settings.Default.initialID = Convert.ToInt32(TempEmployeArray[0, i]);
                    }
                }


                Properties.Settings.Default.Save();
                loadingConfig();
            }
            else
            {
                MessageBox.Show("Mauvais paramétrage de la connection.");
                txtTable.Text = "solut953_tdl";
                txtUser.Text = "solut953_";
                txtServer.Text = "www.solutionob.com";
                txtPass.Password = "";
            }
        }
        private bool testLogIn()
        {
            return GCGGeneralLib.DataBaseConnection.DbTestConnection("server=" + txtServer.Text + ";User Id=" + txtUser.Text + ";password=" + txtPass.Password + ";Persist Security Info=True;database=" + txtTable.Text);
        }
    }
}
